package com.yzt.dynamicmodule.dialog;

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.yzt.dynamicmodule.R;

import com.yzt.dynamicmodule.adapter.DynamicMsgAdapter;
import com.yzt.p_base.utils.KeyboardUtils;
import com.yzt.zhuju.arouter.RouterUser;
import com.yzt.zhuju.dialog.BottomBaseDialog;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.DynamicCommentBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicListBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicMsgBaseBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicMsgBean;
import com.yzt.zhuju.network.entity.dynamic.DynamicMsgChildBean;
import com.yzt.zhuju.utils.FastClickUtils;

/**
 * 作者：bobwang
 * 创建时间：2023-12-15 00:30:32
 * 描述：
 */
public class DynamicCommentDialog extends BottomBaseDialog implements View.OnClickListener {

    private EditText editMsg;
    private Space keyBoardHeight;

    private DynamicMsgAdapter adapter;

    private DynamicCommentBean dynamicCommentBean;

    private String dynamicId;

    private TextView title;

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_dynamic_comment;
    }

    @Override
    public void bindView(View v) {
        initClick();
        initRecycle();
        initInputContainer();
        dynamicId = getArguments().getString("extra_dynamic_id");
        getDynamicComment();
        dynamicCommentBean = new DynamicCommentBean(1, null, null, null, null);
    }

    /**
     * 输入框容器
     */
    private void initInputContainer() {
        LinearLayout llFullContainer = findViewById(R.id.layout_chatroom_edit);
        llFullContainer.setOnClickListener(this);
        // 输入框
        editMsg = findViewById(R.id.chatroom_msg_edit);
        keyBoardHeight = findViewById(R.id.key_board_height);
        // 发送按钮
        findViewById(R.id.tv_chatroom_msg_send).setEnabled(true);
        findViewById(R.id.tv_chatroom_msg_send).setOnClickListener(this);
    }

    private void initClick() {

        title = findViewById(R.id.title);
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dynamicCommentBean = new DynamicCommentBean(1, null, null, null, null);
                showInputView();
            }
        });
        findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
            }
        });
    }

    private void initRecycle() {

        RecyclerView recycler_view = findViewById(R.id.recycler_view);
        recycler_view.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new DynamicMsgAdapter();
        recycler_view.setAdapter(adapter);
        adapter.addChildClickViewIds(R.id.img_header, R.id.tv_reply, R.id.tv_num_replies);
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter1, @NonNull View view, int position) {

                if (FastClickUtils.isFastClick()) {
                    return;
                }

                DynamicMsgBean msgBean = adapter.getItem(position);
                if (view.getId() == R.id.img_header) {
                    // 点击头像
                    RouterUser.PersonalDetailActivity(msgBean.getCommentUserId() + "", "动态");
                }
                if (view.getId() == R.id.tv_reply) {
                    // 点击回复
                    DynamicMsgBean bean = adapter.getItem(position);
                    dynamicCommentBean = new DynamicCommentBean(2, bean.getCommentId(), null, null, null);
                    editMsg.setHint("Reply " + bean.getNickName());
                    showInputView();
                }
                if (view.getId() == R.id.tv_num_replies) {
                    msgBean.setShowAll(!msgBean.isShowAll());
                    adapter.notifyItemChanged(position);
                }
            }
        });
        adapter.setDynamicMsgClickListener(new DynamicMsgAdapter.DynamicMsgClickListener() {
            @Override
            public void childItemClick(DynamicMsgChildBean bean, DynamicMsgBean item) {
                // 点击消息
                dynamicCommentBean = new DynamicCommentBean(3, item.getCommentId(), null, bean.getReplyCommentId(), bean.getCommentUserId());
                editMsg.setHint("Reply " + bean.getNickName());
                showInputView();
            }

            @Override
            public void childLikeClick(DynamicMsgChildBean bean, DynamicMsgBean item) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.tv_chatroom_msg_send) {
            String content = editMsg.getText().toString().trim();
            if (dynamicCommentBean != null) {
                dynamicCommentBean.setContent(content);
                addComment(dynamicCommentBean);
            }
        }
    }

    @Override
    public void mSoftInputHeight(int height) {
        super.mSoftInputHeight(height);
        changeInputHeight(height);
    }

    /**
     * 输入框根据键盘移动
     */
    public void changeInputHeight(int height) {
        ViewGroup.LayoutParams layoutParams = keyBoardHeight.getLayoutParams();
        layoutParams.height = height;
        keyBoardHeight.setLayoutParams(layoutParams);
        if (height == 0 && findViewById(R.id.layout_chatroom_edit).getVisibility() == View.VISIBLE) {
            hideInputView();
        }
    }

    /**
     * 显示输入框
     */
    public void showInputView() {
        KeyboardUtils.showSoftInput(editMsg);
    }

    /**
     * 隐藏输入框
     */
    public void hideInputView() {
        KeyboardUtils.hideSoftInput(editMsg);
    }

    /**
     * 获取评论列表
     */
    private void getDynamicComment() {
        BizLayer.getInstance().getDynamicModule().dynamicCommentList(dynamicId, 100, 1, new AresSubscriber<DynamicMsgBaseBean>() {
            @Override
            protected void onSuccess(DynamicMsgBaseBean response) {
                title.setText(response.getCommentList().size() + "条评论");
                adapter.setList(response.getCommentList());
            }
        });
    }

    /**
     * 添加评论
     */
    private void addComment(DynamicCommentBean bean) {
        if (bean.getStatus() == 1) {
            BizLayer.getInstance().getDynamicModule().dynamicCommentAdd(dynamicId, bean, new AresSubscriber<DynamicMsgBean>() {
                @Override
                protected void onSuccess(DynamicMsgBean response) {
                    dynamicCommentBean = new DynamicCommentBean(1, null, null, null, null);
                    getDynamicComment();
                    hideInputView();
                    editMsg.setText("");
                    editMsg.setHint("请输入");
                }
            });
        } else {
            BizLayer.getInstance().getDynamicModule().dynamicCommentAddChild(dynamicId, bean, new AresSubscriber<DynamicMsgChildBean>() {
                @Override
                protected void onSuccess(DynamicMsgChildBean response) {
                    dynamicCommentBean = new DynamicCommentBean(1, null, null, null, null);
                    getDynamicComment();
                    hideInputView();
                    editMsg.setText("");
                    editMsg.setHint("请输入");
                }
            });
        }
    }
}
